/*
 * @Author: lusx 
 * @Date: 2020-05-08 11:46:31 
 * @Last Modified by: lusx
 * @Last Modified time: 2020-05-19 18:52:29
 */
import Validator from 'validatorjs/dist/validator.min.js';
import { Bubble } from 'weaver-mobile';
 const validatorRegister =(form) => {
    let { startdate = '', starttime = '', endtime = '', astartdate = '', aenddate = '', astarttime = '', aendtime = '' } = form.formParams;
    startdate = startdate.padStart(10, '1000-01-01');
    astartdate = astartdate.padStart(10, '1000-01-01');
    starttime = starttime.padStart(4, '00:00');
    astarttime = astarttime.padStart(4, '00:00');
    endtime = endtime.padStart(4, '23:59');
    aendtime = aendtime.padStart(4, '23:59');
    const start = startdate.split(" ")[0].concat(" ", starttime);
    const astart = astartdate.split(" ")[0].concat(" ", astarttime);
    Validator.register('endafterdate', function (value) {
        value = value.split(" ")[0].concat(" ", endtime);
        return new Date(start.replace(/-/g, "/")) <= new Date(value.replace(/-/g, "/"));
    }, 'The :Start date time cannot be greater than attribute time');
    Validator.register('aendafterdate', function (value) {
        value = value.split(" ")[0].concat(" ", aendtime);
        return new Date(astart.replace(/-/g, "/")) <= new Date(value.replace(/-/g, "/"));
    }, 'The :aStart date time cannot be greater than attribute time');
}
function preValidator(form,formname) {
    let validResult = form.doValidate();
    if (validResult.pass) {
        return true;
    } else {
        Bubble.error(formname+" "+validResult.message);
        return false;
    }
}
function validator(form,formname ="") {
    validatorRegister(form);
    const data = form.formParams;
    const rules = {
        enddate: 'endafterdate',
        aenddate: 'aendafterdate',
    };
    const validation = new Validator(data, rules);
    if (validation.passes()) {
        return Promise.resolve(preValidator(form,formname));
    } else if (validation.fails()) {
        Bubble.warning('开始日期时间不能大于结束日期时间', 2, {}, 1);
        return Promise.resolve(false);
    }
}
function sleep(interval) {
    return new Promise(resolve => {
        setTimeout(resolve, interval);
    })
}
async function sleepInAsync({ interval, params }) {
    await sleep(interval);
    return params;
}
export { validator as validatorTool, sleepInAsync,validatorRegister }